﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using King.Text;
using King.Extensions;
using King.Declarative;

namespace King.Automation {

    public sealed class TextCommandLine : TextDeclaration {

        private object m_currentDirectory;

        public TextCommandLine(object currentDirectory = null) {
            m_currentDirectory = currentDirectory;
        }

        protected override IEnumerable Children(DeclarationContext frame) {
            return new TextJoin(" ", prefix: m_currentDirectory) {
                frame.Flatten<TextCommandLineArgument>(Children(), 
                    o => new TextCommandLineArgument() { o }
                )
            };
        }
    }
    public sealed class TextCommandLineArgument : TextDeclaration {
        
        protected override IEnumerable Children(DeclarationContext frame) {

            var argument = frame.ToString(Children());
            var useQuotes = !(argument.StartsWith("\"") && argument.EndsWith("\"")) && argument.Any(o => char.IsWhiteSpace(o));

            if (useQuotes)
                yield return new TextDeclaration() { "\"", Children(), "\"" };
            else
                yield return Children();
        }
    }
}
